/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.common;

import java.util.ArrayList;
import java.util.List;
import com.example.TravelApp.R;
import com.example.TravelApp.search.common.helper.MapDrawPathHelper;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

/**
 * This activity is displays all the points of interests suggested to the user
 * on the map using overlays. The user can click on any point view details of
 * that point in a separate dialog box
 * 
 * @author Mani Brundha and Haritha Subramanyam
 * 
 */
public class MapListOfOptionsActivity extends
		com.google.android.maps.MapActivity {

	private ArrayList<PlaceObject> list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Retrieve places list from intent
		Intent myIntent = getIntent();
		this.list = myIntent.getParcelableArrayListExtra("placesList");
		if (this.list.isEmpty() != true) {

			setContentView(R.layout.display_list_options);
			MapView mapView = (MapView) findViewById(R.id.mapview);
			mapView.setBuiltInZoomControls(true);
			List<Overlay> mapOverlays = mapView.getOverlays();
			Drawable drawable = this.getResources().getDrawable(
					R.drawable.map_pin_green);
			PlotItemizedOverlay itemizedoverlay = new PlotItemizedOverlay(
					drawable, this);

			List<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
			MapController mc = mapView.getController();

			String venueName = " ", rating = " ", address = " ";

			// Iterate through the entire list of places and plot overlays
			// With address,venue name and rating information

			for (int i = 0; i < list.size(); i++) {
				if (!list.get(i).getLatitudeAndLongitude().equals("null,null")) {
					// construct a geopoint out of the string
					GeoPoint newGeoPoint = MapDrawPathHelper.getGeoPoint(list
							.get(i).getLatitudeAndLongitude());
					geoPoints.add(i, newGeoPoint);
					if (list.get(i).getVenueName() != null)
						venueName = list.get(i).getVenueName();
					if (list.get(i).getAddress() != null)
						address = list.get(i).getAddress();
					if (list.get(i).getRating() != null)
						rating = list.get(i).getRating();
					OverlayItem overlayitem = new OverlayItem(geoPoints.get(i),
							venueName + "\n" + address, rating);
					itemizedoverlay.addOverlay(overlayitem);
					mapOverlays.add(itemizedoverlay);
					mc.animateTo(geoPoints.get(i));
				} else {
					geoPoints.add(i, null);
				}
			}
		} else {
			// Displays No co-ordinates
			setContentView(R.layout.no_results);
		}

	}

	// @Override
	protected boolean isRouteDisplayed() {
		return false;
	}

}
